package com.usian.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.dtos.AdChannelDto;
import com.usian.mapper.ChannelMapper;
import com.usian.pojo.AdChannel;
import com.usian.result.Result;
import com.usian.service.ChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@SuppressWarnings("ALL")
public class ChannelServiceImpl implements ChannelService {

    @Autowired
    private ChannelMapper channelMapper;

    @Override
    public Result findBySearch(AdChannelDto adChannelDto) {
        Example example = new Example(AdChannel.class);
        // where
        Example.Criteria criteria = example.createCriteria();
        //只判断错 不判断对 防止嵌套
        if (adChannelDto == null) {
            //报错
            throw new RuntimeException("失败");
        }
        // 1. 模糊查询
        if (adChannelDto.getName() != null && !"".equals(adChannelDto.getName())) {
            // 拼接条件 实体类中的属性名 mybatis Orm  对象 （Java对象） ==》 关系 （SQL语句）
            // and like %?%
            criteria.andLike("name", "%" + adChannelDto.getName() + "%");
        }
        // 2. 精准查询
        if (adChannelDto.getOrd() != null) {
            //and ord == ?
            criteria.andEqualTo("ord", adChannelDto.getOrd());
        }
        // 3. 时间>=  当前时间 >= startTime
        // gt gte lt lte ne
        if (adChannelDto.getStartTime() != null) {
            criteria.andGreaterThanOrEqualTo("createdTime", adChannelDto.getStartTime());
        }
        // 4. 时间<=  当前时间 <= endTime
        if (adChannelDto.getEndTime() != null) {
            criteria.andGreaterThanOrEqualTo("createdTime", adChannelDto.getEndTime());
        }

        // 防止空指针 需要自行判断


        //查询结果（根据条件查询）  limit(0,10)  (page-1)*size  size
        PageHelper.startPage(adChannelDto.getPage(), adChannelDto.getSize());
        List<AdChannel> list = channelMapper.selectByExample(example);
        //分页条件
        PageInfo<AdChannel> pageInfo = new PageInfo<>(list);

        Result result = new Result();
        result.setCode(20000);
        result.setMsg("查询成功");
        result.setData(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());

        return result;
    }

    @Override
    public Result findOne(Integer id) {
        if (id == null) {
            //报错
            throw new RuntimeException("失败");
        }
        Result result = new Result();
        result.setCode(20000);
        result.setMsg("查询成功");
        result.setData(channelMapper.selectByPrimaryKey(id));
        return result;
    }



    @Override
    public Result save(AdChannel adChannel) {
        if(adChannel.getId() == null ||adChannel.getId() == 0){
            channelMapper.insert(adChannel);
        }else {
            channelMapper.updateByPrimaryKey(adChannel);
        }
        return new Result(null);
    }
}
